<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>部门列表</title>
    <script type="text/javascript">
      var newDepartmentWin;
      var editDepartmentWin;
      var departmentTree;
      Ext.onReady(function(){
        departmentTree = new Ext.tree.ColumnTree({
          width: screenWidth-200,
          height: screenHeight-285,
          rootVisible:true,
          autoScroll:true,
          title: '部门(分公司)列表',
          renderTo: 'departments_tree_div',
          columns:[{
              header:'名称',
              width:330,
              dataIndex:'id'
            },{
              header:'编号',
              width:100,
              dataIndex:'number'
            },{
              header:'类型',
              width:60,
              dataIndex:'sub_type'
            },{
              header:'是否锁定',
              width:100,
              renderer: renderDeptIsLucked,
              dataIndex:'is_locked'
            },{
              header:'备注',
              width:300,
              dataIndex:'description'
            }],

          loader: new Ext.tree.TreeLoader({
            dataUrl:'/department/getAllDepartmentsForTree',
            uiProviders:{
              'col': Ext.tree.ColumnNodeUI
            }
          }),

          root: new Ext.tree.AsyncTreeNode({
            id:0,
            text:'总公司'
          }),
          bbar: [{
              text: '刷新整树',
              iconCls: 'refresh',
              handler : function(){
                departmentTree.root.reload();
              }
            }]
        });

        //锁定仓库类型
        function renderDeptIsLucked(value, cellmeta, record, rowIndex, columnIndex, store){
          if (record.is_locked=="1"){
            return "<a style=cursor:pointer )><font color=red>锁定</font></a>";
          }else{
            return "<a style=cursor:pointer ><font color=green>未锁定</font></a>";
          }
        }
        
        departmentTree.on("contextmenu",function(node,e){
          e.preventDefault();
          DepartmentRightMenu.showAt(e.getXY());
          //DepartmentRightMenu.show(node.ui.getAnchor());
          node.select();
          currentNode = node;
          nodeId = currentNode.id;
          //window.status = currentNode.id;
          //alert(typeof(currentNode.id)==="string");
          //如果为叶子，则禁止刷新功能。
          if (node.isLeaf()){
            refreshMenuItem.setDisabled(true);
          }else{
            if (nodeId == 0){
              editDepartmentMenuItem.setDisabled(true);
              delDepartmentMenuItem.setDisabled(true);
            }
            refreshMenuItem.setDisabled(false);
          }
          //如果不是根上的叶或节点，禁止修改编辑,string为非根节点上的直接叶或节点。
          if (typeof(currentNode.id)==="string"){
            editDepartmentMenuItem.setDisabled(true);
            delDepartmentMenuItem.setDisabled(true);
          }else if (typeof(currentNode.id)==="number" && nodeId != 0){
            editDepartmentMenuItem.setDisabled(false);
            delDepartmentMenuItem.setDisabled(false);
          }
        },this);

        //右键菜单
        var addDepartmentMenuItem = new Ext.menu.Item({
          text: '添加部门(店铺、分公司)',
          iconCls: 'addItem',
          handler: rightMenuDepartmentTreeFn
        });
        var editDepartmentMenuItem = new Ext.menu.Item({
          text: '编辑部门(店铺、分公司)',
          iconCls: 'editItem',
          handler: rightMenuDepartmentTreeFn
        });
        var delDepartmentMenuItem = new Ext.menu.Item({
          text: '删除',
          iconCls: 'remove',
          handler: rightMenuDepartmentTreeFn
        });
        var lockMenuItem = new Ext.menu.Item({
          text: '锁定或解锁',
          iconCls: 'lock',
          handler: rightMenuDepartmentTreeFn
        });
        var refreshMenuItem = new Ext.menu.Item({
          text: '刷新',
          iconCls: 'refresh',
          handler: rightMenuDepartmentTreeFn
        });

        var DepartmentRightMenu = new Ext.menu.Menu({
          //id: 'Department_right_menu',
          items: [
            addDepartmentMenuItem,
            editDepartmentMenuItem,
            '-',
            delDepartmentMenuItem,
            '-',
            lockMenuItem,
            '-',
            refreshMenuItem
          ]});
        function rightMenuDepartmentTreeFn(item,e){
          if (item.text=="添加部门(店铺、分公司)"){
            newDepartmentWin = new Ext.Window({
              layout: 'fit',
              width: 318,
              height: 240,
              closeAction: 'destroy',
              plain: true,
              modal: true,
              constrain:true,
              //modal: true,
              title: '添加部门(店铺、分公司)',
              autoLoad: {
                url: "/department/newDepartment?parent_id="+nodeId,
                scripts: true
              }
            });
            newDepartmentWin.on("destroy",function(){
              //刷新整个树
              departmentTree.root.reload();
            });
            newDepartmentWin.resizable = false;
            newDepartmentWin.show();
          }else if (item.text=="编辑部门(店铺、分公司)"){
            editDepartmentWin = new Ext.Window({
              layout: 'fit',
              width: 318,
              height: 240,
              closeAction: 'destroy',
              plain: true,
              modal: true,
              constrain:true,
              //modal: true,
              title: '编辑部门(店铺、分公司)',
              autoLoad: {
                url: "/department/editDepartment?id=" + currentNode.id,
                scripts: true
              }
            });
            editDepartmentWin.on("destroy",function(){
              //刷新整个树
              departmentTree.root.reload();
            });
            editDepartmentWin.resizable = false;
            editDepartmentWin.show();
          }else if (item.text=="刷新"){
            if (currentNode.isLeaf()){
              alert("是一个Child");
            }else{
              //判断是否导入过，就是是否被人点击过，因为这是Ajax的，一但点击，就会导入数据。
              if (currentNode.isLoaded()){
                //如果导入过，就要刷新后，再expand();
                departmentTree.loader.dataUrl = "/department/getTreeData?node=" + currentNode.id; //定义子节点的Loader
                departmentTree.loader.load(currentNode,expandCurrentNode);
              }else{
                //如果没有导入过，触发expand事件，就可以导入，相当于刷新。
                currentNode.expand();
              }
              //tree.getRootNode().select();
              window.state = currentNode.id;
              //currentNode.reload();
            }
          }else if(item.text == "删除"){
            Ext.MessageBox.confirm("提示","你确认删除 \""+currentNode.text+"\" 吗？",function(btn){
              if(btn=="yes"){
                Ext.Ajax.request({
                  url : '/department/destroy?id=' + currentNode.id,
                  success : function (result, request) {
                    departmentTree.root.reload();
                  },
                  failure : function (result, request){
                    Ext.MessageBox.show({
                      title: '消息',
                      msg: "通讯失败，请从新操作",
                      buttons: Ext.MessageBox.OK,
                      icon: Ext.MessageBox.WARNING
                    });
                  },
                  method : 'GET'
                });
              }else{
                //alert("no");
              }
            });
          }else if (item.text=="锁定或解锁"){
            //处理销售类型锁定
            Ext.Ajax.request({
              url : '/department/departmentLock',
              success : function (result, request) {
                //刷新整个树
                departmentTree.root.reload();
              },
              failure : function (result, request){
                Ext.MessageBox.show({
                  title: '消息',
                  msg: "通讯失败，请从新操作",
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.WARNING
                });
              },
              method : 'GET',
              params : {
                id : currentNode.id
              }
            });
          }
        }

        function expandCurrentNode(){
          //alert(currentNode.id);
          currentNode.expand();
          //currentNode.cascade();
        }


        departmentTree.on('beforeload',
        function(node){
          departmentTree.loader.dataUrl='/department/getAllDepartmentsForTree?node='+node.id; //定义子节点的Loader
        });
        
        departmentTree.root.id = 0;
        departmentTree.root.expand();
        departmentTree.render();
      })
    </script>
  </head>
  <body>
    <div id="departments_tree_div"></div>
  </body>
</html>
